package com.wzc.pet.controller;

import com.wzc.basic.exception.BusinessException;
import com.wzc.basic.util.AjaxResult;
import com.wzc.basic.util.LoginContext;
import com.wzc.basic.util.PageList;
import com.wzc.pet.domain.SearchMasterMsg;
import com.wzc.pet.dto.AcceptDto;
import com.wzc.pet.query.SearchMasterMsgQuery;
import com.wzc.pet.service.ISearchMasterMsgService;
import com.wzc.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController   // @Controller 和
@RequestMapping("/searchMasterMsg")
@Api(value = "寻主接口类",description = "寻主相关CRUD")
public class SearchMasterMsgController {


    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;

    /**
     * 查询所有
     * @GetMapping
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有")
//    @PreAuthorize(name = "寻主管理查询所有", sn = "searchMsg:list")
    public List<SearchMasterMsg> loadAll(HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        return searchMasterMsgService.loadAll();
    }


    /**
     * 查询单个
     * @GetMapping("/{id}") 后端接收前端传输的数据（动态传输）
     * @PathVariable("id") 从路径上去获取id变量（后端取数据）再赋值给 Long id
     * @param id
     * @return
     */
    @GetMapping("/{id}")  // 接收参数 @PathVariable获取参数
    @ApiOperation(value = "通过id查询单个数据")
//    @PreAuthorize(name = "寻主管理通过id查询单个数据", sn = "searchMsg:loadById")
    public SearchMasterMsg loadById(@PathVariable("id") Long id){
        return searchMasterMsgService.loadById(id);
    }

    /**
     * @RequestBody  从请求体中获取数据赋值给searchMsgQuery
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询")
//    @PreAuthorize(name = "寻主管理分页查询", sn = "searchMsg:queryPage")
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query){
        try {
            return searchMasterMsgService.queryPage(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }
    }


    /**
     * 删除
     * @DeleteMapping("/{id}") 后端接收前端参数
     * @PathVariable("id")  从资源路径上获取参数id，在赋值给Long id
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
//    @PreAuthorize(name = "寻主管理删除操作", sn = "searchMsg:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
//    @PreAuthorize(name = "寻主管理批量删除操作", sn = "searchMsg:patchDelete")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            searchMasterMsgService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setMsg("操作失败");
        }

    }




    /**
     * 添加和修改
     * @PutMapping 请求体携带参数
     * @RequestBody  从请求体中获取参数
     * @param searchMsg
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改")
//    @PreAuthorize(name = "寻主管理添加或修改", sn = "searchMsg:saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody SearchMasterMsg searchMsg){
        try {
            if (searchMsg.getId() != null){
                searchMasterMsgService.update(searchMsg);
            } else {
                searchMasterMsgService.save(searchMsg);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("操作失败");
        }
    }


    @PostMapping("/publish")
    @ApiOperation(value = "寻主")
//    @PreAuthorize(name = "寻主管理分页查询", sn = "searchMsg:queryPage")
    public AjaxResult publish(@RequestBody SearchMasterMsg smm, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            searchMasterMsgService.publish(smm, logininfo.getId());
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("网络异常！controller");
        }
    }


    @PostMapping("/toHandle")
    @ApiOperation(value = "待处理查询接口")
//    @PreAuthorize(name = "待处理查询接口", sn = "searchMsg:queryPage")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            return searchMasterMsgService.toHandle(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }
    }

//    @PostMapping("/user")
//    @ApiOperation(value = "待处理查询接口")
//    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
//        try {
//            Logininfo logininfo = LoginContext.getLogininfo(request);
//            return searchMasterMsgService.user(query,logininfo.getId());
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new PageList<SearchMasterMsg>();
//        }
//    }


    /**
     * 拒单
     * @param
     * @param
     * @return
     */
    @GetMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.reject(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("网络异常！");
        }
    }


    /**
     * 接单
     * @param
     * @param
     * @return
     */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody AcceptDto dto, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            searchMasterMsgService.accept(dto, logininfo.getId());
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("网络异常！");
        }
    }
}
